
package com.yadong.smarthome2.devicefinder;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.os.Handler;

import com.yadong.smarthome2.device.Device;
import com.yadong.smarthome2.protocol2.ALog;

public class DeviceFinderWorker implements DeviceListener {
    private static final int SCAN_DELAY = 30 * 1000;
    private static final int MSG_STOP_SCAN = 1;
    private static final Class<?>[] DEVICE_FINDER =
            new Class<?>[] {WifiDeviceFinder.class, BluetoothDeviceFinder.class};

    private List<DeviceFinder> mWorkingFinder = new ArrayList<DeviceFinder>();

    private DeviceListener mListener;
    private ScanListener mScanListener;
    private boolean mIsWorking = false;
    private Context mContext;

    public interface ScanListener {
        public void onScanStart();
        public void onScanEnded();
    }

    public DeviceFinderWorker(Context context, DeviceListener deviceListener,
            ScanListener scanListener) {
        mContext = context;
        mListener = deviceListener;
        mScanListener = scanListener;
    }

    public boolean isWorking() {
        return mIsWorking;
    }

    private void markAsWorking() {
        //delay 30s to stop scan.
        mHandler.sendEmptyMessageDelayed(MSG_STOP_SCAN, SCAN_DELAY);
        mIsWorking = true;
        if(mScanListener != null) {
            mScanListener.onScanStart();
        }
    }

    private void markAsStop() {
        //stop now. remove delayed message
        if(mHandler.hasMessages(MSG_STOP_SCAN)) {
            mHandler.removeMessages(MSG_STOP_SCAN);
        }
        mIsWorking = false;
        if(mScanListener != null) {
            mScanListener.onScanEnded();
        }
        //When scan finish, remove
        mScanListener = null;
    }

    public void startScan() {
        ALog.logd(this, "start scan");
        markAsWorking();
        for (Class<?> finderClass : DEVICE_FINDER) {
            try {
                DeviceFinder deviceFinder = (DeviceFinder) finderClass.newInstance();
                if (deviceFinder instanceof BluetoothDeviceFinder) {
                    ((BluetoothDeviceFinder)deviceFinder).setContext(mContext);
                }
                deviceFinder.scanDevice(this);
                mWorkingFinder.add(deviceFinder);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    public void stopScan() {
        ALog.logd(this, "stop scan");
        markAsStop();
        for (DeviceFinder deviceFinder : mWorkingFinder) {
            deviceFinder.stopScan();
        }
        mWorkingFinder.clear();
    }

    private Handler mHandler = new Handler() {
        public void handleMessage(android.os.Message msg) {
            switch (msg.what) {
                case MSG_STOP_SCAN:
                    stopScan();
                    break;
                default:
                    break;
            }
        };
    };

    public void onDeviceFinded(Device device) {
        ALog.logd(this, "on device finded :" + device);
        if (mListener != null) {
            mListener.onDeviceFinded(device);
        }
    }
}
